using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Object = UnityEngine.Object;

public class GameObjectPool : PoolBase
{
    public override Object Spwan(string name)
    {
        Object obj = base.Spwan(name);
        if (obj == null)
            return null;
        GameObject go = obj as GameObject;
        go.SetActive(true);
        return go;
    }

    public override void UnSpwan(string name, Object obj)
    {
        GameObject go = obj as GameObject;
        go.SetActive(false);
        go.transform.SetParent(this.transform, false);
        base.UnSpwan(name, obj);
    }

    //释放函数，再update中定期执行，资源的上次使用时间距现在过长时，释放
    public override void Release()
    {
        base.Release();
        foreach(PoolObject poolObject in m_Pool)
        {
            if(DateTime.Now.Ticks - poolObject.LastUseTime.Ticks >= m_ReleaseTime * 10000000)
            {
                //先销毁游戏对象
                Destroy(poolObject.Object);
                Manager.Resource.MinusBundleCount(poolObject.Name);
                m_Pool.Remove(poolObject);
                //如果在遍历集合的时候移除元素，可能会导致迭代器失效引发异常。因此一旦触发移除之后，就需要递归重新进行检测并移除,
                //移除一个之后不遍历直接退出遍历
                Release();
                return;
            }
        }
    }
}
